﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GFluentCode.Tools
{
    /// <summary>
    /// 日期时间扩展
    /// </summary>
    public static class DateTimeExtension
    {
        /// <summary>
        /// 转换为当天零点
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static DateTime GToMorning(this DateTime dt)
        {
            return DateTime.Parse(dt.ToString("yyyy-MM-dd 00:00:00"));
        }

        /// <summary>
        /// 转换为当天最后一个时刻
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static DateTime GToNight(this DateTime dt)
        {
            return DateTime.Parse(dt.ToString("yyyy-MM-dd 23:59:59"));
        }

        /// <summary>
        /// 转完整24小时制字符串
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string GToFullString(this DateTime dt)
        {
            return dt.ToString("yyyy-MM-dd HH:mm:ss");
        }

        /// <summary>
        /// 转日期
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string GToDateString(this DateTime dt)
        {
            return dt.ToString("yyyy-MM-dd");
        }

        /// <summary>
        /// 转24小时制时间字符串
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string GTo24TimeString(this DateTime dt)
        {
            return dt.ToString("HH:mm:ss");
        }

        /// <summary>
        /// 转12小时制时间字符串
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string GTo12TimeString(this DateTime dt)
        {
            return dt.ToString("hh:mm:ss");
        }

        /// <summary>
        /// 转日期目录
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string GToDateDirectory(this DateTime dt)
        {
            return dt.ToString("yyyyMMdd");
        }

        /// <summary>
        /// 转时间目录
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string GToTimeDirectory(this DateTime dt)
        {
            return dt.ToString("HHmmss");
        }

        /// <summary>
        /// 将两个时间转为 xx天xx小时xx分
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="endDate"></param>
        /// <returns></returns>
        public static string GToDiffTimeString(this DateTime dt,DateTime endDate)
        {
            var ts = endDate - dt;

            var str = string.Empty;

            if ((int)ts.TotalDays > 0) str = $"{((int)ts.TotalDays)}天";

            if (ts.Hours > 0) str += $"{(int)ts.Hours}小时";

            if (ts.Minutes > 0) str+=$"{(int)ts.Minutes}分";

            return str;
        }

        /// <summary>
        /// 数据Join
        /// </summary>        
        /// <param name="collections"></param>
        /// <param name="separator"></param>
        /// <returns></returns>
        public static string GJoin<T>(this IEnumerable<T> collections,string separator = "")
        {
            if (collections == null) return string.Empty;

            return string.Join<T>(separator, collections);
        }
    }
}
